package com.cays.myresearch.Util;

import android.os.Handler;
import android.os.Message;

import com.cays.myresearch.constant.Constant;
import com.cays.myresearch.entity.ResponseInfo;
import com.google.gson.reflect.TypeToken;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by lenovo on 2018/2/23.
 */

public class HttpUtil {
    public static void sendHttpRequest(String address,Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url(address)
                .build();
        client.newBuilder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS);
        client.newCall(request).enqueue(callback);
    }
    public static void sendHttpRequest(String address, RequestBody requestBody, Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request request=new Request.Builder()
                .url(address)
                .post(requestBody)
                .build();
        client.newBuilder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS);
        client.newCall(request).enqueue(callback);
    }
    public static void sendHttpRequest(String address, MultipartBody.Builder builder, Callback callback){
        OkHttpClient client=new OkHttpClient();
        RequestBody body=builder.build();
        Request request=new Request.Builder()
                .url(address)
                .post(body)
                .build();
        client.newBuilder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS);
        client.newCall(request).enqueue(callback);
    }
    public static void sendFileHttpRequest(String address, HashMap<String,String> fileNames,Callback callback){
        OkHttpClient client=new OkHttpClient();
        MultipartBody.Builder builder=new MultipartBody.Builder();
        //设置类型
        builder.setType(MultipartBody.FORM);
        //追加参数
        for (String key : fileNames.keySet()) {
            String str= fileNames.get(key);
            if (key.equals("packageInfo")) {
                builder.addFormDataPart(key, str);
            } else {
                File file = new File(str);
                if(!file.exists()) try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

                builder.addFormDataPart(key,file.getName(),fileBody);
            }
        }
        //创建RequestBody
        RequestBody body = builder.build();
        Request request=new Request.Builder()
                .url(address)
                .post(body)
                .build();
        client.newBuilder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .readTimeout(5,TimeUnit.SECONDS)
                .writeTimeout(5,TimeUnit.SECONDS);
        client.newCall(request).enqueue(callback);
    }
    public static void sendHttpRequest(final String url,
                                       final Handler handler, final int success, final int failed){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendHttpRequest(url, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Message message=new Message();
                        message.what=0;handler.sendMessage(message);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String s=response.body().string();
                        Message message=new Message();
                        if (!s.isEmpty()){
                            message.what=success;message.obj=s;
                            handler.sendMessage(message);
                        }else{
                            message.what=failed;handler.sendMessage(message);
                        }
                    }
                });
            }
        }).start();
    }
    public static void sendHttpRequest(final String url,final MultipartBody.Builder builder,
                                       final Handler handler, final int success, final int failed){
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendHttpRequest(url,builder, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Message message=new Message();
                        message.what=0;handler.sendMessage(message);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String s=response.body().string();
                        Message message=new Message();
                        if (!s.isEmpty()){
                            message.what=success;message.obj=s;
                            handler.sendMessage(message);
                        }else{
                            message.what=failed;handler.sendMessage(message);
                        }
                    }
                });
            }
        }).start();
    }
}
